package stackdemo;

import stackdemo.MyStack;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-01-22
 * Time: 18:35
 */
public class Test {

    public boolean IsPopOrder(int[] pushV,int[] popV){
        Stack<Integer> stack = new Stack<>();

        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (!stack.empty() && j < popV.length
             && stack.peek() == popV[j]){
                stack.pop();
                j++;
            }
        }
        return stack.empty();
    }

    //有效的括号
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);

            if (ch == '(' || ch == '{' || ch == '['){
                stack.push(ch);
            }else {
                //ch = 右括号
                if (stack.empty()){
                    return false;
                }
                char peekCh = stack.peek();
                if ((peekCh == '(' && ch == ')')||(peekCh == '{' && ch == '}')||(peekCh == '[' && ch == ']')){
                    stack.pop();
                }else {
                    return false;
                }

            }
        }
        if (!stack.empty()){
            return false;
        }
        return true;
    }

    public int evalRPN(String[] tokens){
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String s = tokens[i];
            if (!isOperation(s)){
                //数字
                Integer ret = Integer.valueOf(s);
                stack.push(ret);
            }
            else {
                Integer val2 = stack.pop();
                Integer val1 = stack.pop();
                switch (s) {
                    case "+":
                        stack.push(val1+val2);
                        break;
                    case "-":
                        stack.push(val1-val2);
                        break;
                    case "*":
                        stack.push(val1*val2);
                        break;
                    case "/":
                        stack.push(val1/val2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    private boolean isOperation(String val){
        if (val.equals("+")||val.equals("-")||val.equals("*")||val.equals("/")){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);
        stack.push(56);
        int val = stack.pop();
        System.out.println(val);
        val = stack.pop();
        System.out.println(val);

        int val2 = stack.peek();
        System.out.println(val2);

        boolean empty = stack.isEmpty();
        System.out.println(empty);
    }

    public static void main1(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(12);
        stack.push(23);
        stack.push(34);
        stack.push(45);
        stack.push(56);
        int val = stack.pop();
        System.out.println(val);
        val = stack.pop();
        System.out.println(val);

        int val2 = stack.peek();
        System.out.println(val2);

        boolean empty = stack.empty();
        System.out.println(empty);
    }
}
